
package com.google.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TimerTask;

import org.gps.domain.AlarmCacheData;
import org.gps.domain.TArea;

import com.dbmanager.DBManager;

public class AlarmInitThread extends TimerTask
{

	public AlarmInitThread()
	{
	}

	public void run()
	{
		System.out.println("----------初始化报警信息开始------------------------");
		long alarmStart = System.currentTimeMillis();
		initAlarmData();
		System.out.println("----------初始化报警信息结束------------------------");
		long alarmEnd = System.currentTimeMillis();
		System.out.println("--------报警初始化信息耗时：-------"+(alarmEnd - alarmStart)+"毫秒");
	}

	public void initAlarmData()
	{
		DBManager dbmanager = DBManager.getInstance();
		Connection conn = dbmanager.getConnection();
		PreparedStatement st = null;
		String sb =" select t.device_id, t.area_id, t.order_id,a.area_type,t.type_id,a.radius,a.limit_time,a.limit_speed,a.xys,a.area_name,a.is_empty,a.alarm_rule,a.stockPile_name"
				+"  from g_vehicle_area t, g_area a"
		        +" where  "
		        +"    t.area_id = a.id"
		        +" order by t.device_id, t.order_id asc";

		try
		{
		    st = conn.prepareStatement(sb, 1005, 1007);
			ResultSet rs = st.executeQuery();
			Long device_id = null;
			HashMap hashMap = new HashMap();
			List list = new ArrayList();
			while (rs.next()) 
			{
				TArea tArea = new TArea();
				tArea.setDeviceId(Long.valueOf(rs.getLong("device_id")));
				tArea.setId(Long.valueOf(rs.getLong("area_id")));
				tArea.setAreaType(rs.getString("area_type"));
				tArea.setRadius(rs.getDouble("radius"));
				tArea.setXys(rs.getString("xys"));
				tArea.setAreaLine(rs.getString("type_id"));
				tArea.setOrderId(rs.getInt("order_id"));
				tArea.setLimitSpeed(rs.getString("limit_speed"));
				tArea.setLimitTime(rs.getString("limit_time"));
				tArea.setAreaName(rs.getString("area_name"));
				tArea.setIsEmpty(rs.getString("is_empty"));
				tArea.setAlarmRule(rs.getString("alarm_rule"));
				tArea.setSticoPileId(rs.getString("stockPile_name"));
				if (rs.isFirst())
				{
					device_id = Long.valueOf(rs.getLong("device_id"));
					list.add(tArea);
				} else
				if (rs.isLast())
				{
					Long deviceId = Long.valueOf(rs.getLong("device_id"));
					if (deviceId.longValue() == device_id.longValue())
					{
						list.add(tArea);
						hashMap.put(device_id, list);
						list = new ArrayList();
					} else
					{
						list.add(tArea);
						hashMap.put(device_id, list);
						list = new ArrayList();
					}
				} else
				{
					Long deviceId = Long.valueOf(rs.getLong("device_id"));
					if (deviceId.longValue() == device_id.longValue())
					{
						list.add(tArea);
					} else
					{
						hashMap.put(device_id, list);
						device_id = deviceId;
						list = new ArrayList();
						list.add(tArea);
					}
				}
			}
			AlarmCacheData.getInstance().setGpsAreaList(hashMap);
		}
		catch (SQLException e)
		{
			System.out.println(e.getMessage());
		}finally{
			try {        		
    			if(st!=null){
    				
    				st.close();
    			}
    			if(conn!=null){
    				conn.close();
    			}
    			
			  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			loadAllArea();
		}
	}

	private void loadAllArea(){
		DBManager dbmanager = DBManager.getInstance();
		Connection conn = dbmanager.getConnection();
		PreparedStatement st = null;
		String sb = "SELECT * FROM (" +
				"SELECT t.type_name,ROWNUM rn,a.id as area_id,a.area_type,a.radius,a.area_name,a.limit_time," +
				"a.limit_speed,a.xys,a.add_Time,a.is_Empty,a.alarm_Rule,a.mark_Type,a.STOCKPILE_NAME" +
				" FROM g_Mark_Type t, g_Area a" +
				" WHERE a.mark_type = t.id(+)" +
				" and a.area_type !=4)";
		try
		{
			st = conn.prepareStatement(sb, 1005, 1007);
			ResultSet rs = st.executeQuery();
			List list = new ArrayList();
			while (rs.next())
			{
				TArea tArea = new TArea();
				tArea.setId(Long.valueOf(rs.getLong("area_id")));
				tArea.setAreaType(rs.getString("area_type"));
				tArea.setRadius(rs.getDouble("radius"));
				tArea.setXys(rs.getString("xys"));
				tArea.setLimitSpeed(rs.getString("limit_speed"));
				tArea.setLimitTime(rs.getString("limit_time"));
				tArea.setAreaName(rs.getString("area_name"));
				tArea.setIsEmpty(rs.getString("is_empty"));
				tArea.setAlarmRule(rs.getString("alarm_rule"));
				list.add(tArea);
			}
			AlarmCacheData.getInstance().setGpsAllAreaList(list);
		}
		catch (SQLException e)
		{
			System.out.println("加载所有责任区异常：" + e.getMessage());
		}finally{
			try {
				if(st!=null){

					st.close();
				}
				if(conn!=null){
					conn.close();
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
